﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Adex;
using MediaMonitor.Common;

namespace MediaMonitor.Client
{
    public partial class Company_Orders : Adex.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Utility.AddDateControlToPage(this.Page);
                FillUsers();
            }
        }

        private void FillUsers()
        {

            DataTable dt = SiteUser.Query().SetSelectList("IsNull(FullName, '') + ' (' + Username + ')' AS DisplayName, SiteUserID, FullName, Username").WHERE("CompanyCode", AppUser.CurrentUser.CompanyCode).ORDER_BY("FullName").ExecuteDataSet().Tables[0];
            lstUser.DataSource = dt;
            lstUser.DataTextField = "DisplayName";
            lstUser.DataValueField = "SiteUserID";
            lstUser.DataBind();

            lstUser.Items.Insert(0, new ListItem("- All -", "0"));
        }

        protected void radGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {

            DataTable dt;
            string sDate = tbDateFrom.Text == "" ? "01/01/2010" : tbDateFrom.Text;
            string eDate = tbDateTo.Text == "" ? "12/31/2020" : Utility.ToDateTime(tbDateTo.Text).AddDays(1).ToShortDateString();
            //string sDate = "01/01/2010";
            //string eDate = "12/31/2020";

            if (lstUser.SelectedValue != "0")
            {
                dt = VOrder.Query().SetSelectList("*, CONVERT(VARCHAR, DateAdded, 101) AS OrderDate").WHERE("CompanyCode", AppUser.CurrentUser.CompanyCode).AND("SiteUserID", lstUser.SelectedValue).BETWEEN_VALUES("DateAdded", sDate, eDate).ORDER_BY("DateAdded DESC").ExecuteDataSet().Tables[0];
            }
            else
            {

                dt = VOrder.Query().SetSelectList("*, CONVERT(VARCHAR, DateAdded, 101) AS OrderDate").WHERE("CompanyCode", AppUser.CurrentUser.CompanyCode).BETWEEN_VALUES("DateAdded", sDate, eDate).ORDER_BY("DateAdded DESC").ExecuteDataSet().Tables[0];
            }

            radGrid.DataSource = dt;
        }


        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            radGrid.Rebind();
        }
    }
}